import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import dts from 'vite-plugin-dts'

export default defineConfig(({ mode }) => {
    const env = loadEnv(mode, process.cwd(), '')

    const isLib = mode === 'lib'

    return {
        plugins: [
            vue(),
            vueJsx(),
            AutoImport({
                imports: ['vue', 'vue-router', 'pinia'],
                dts: true,
            }),
            Components({
                resolvers: [
                    AntDesignVueResolver({
                        importStyle: false,
                    }),
                ],
                dts: true,
            }),
            isLib && dts({
                include: ['src/components/**/*'],
                outDir: 'dist/types'
            })
        ].filter(Boolean),

        resolve: {
            alias: {
                '@': resolve(__dirname, 'src'),
                '@components': resolve(__dirname, 'src/components'),
                '@utils': resolve(__dirname, 'src/utils'),
                '@stores': resolve(__dirname, 'src/stores'),
                '@views': resolve(__dirname, 'src/views'),
                '@api': resolve(__dirname, 'src/api'),
                '@assets': resolve(__dirname, 'src/assets'),
            },
        },

        css: {
            preprocessorOptions: {
                less: {
                    modifyVars: {
                        '@primary-color': '#1890ff',
                        '@link-color': '#1890ff',
                        '@border-radius-base': '6px',
                    },
                    javascriptEnabled: true,
                },
            },
        },

        build: isLib ? {
            lib: {
                entry: resolve(__dirname, 'src/components/index.ts'),
                name: 'EnterpriseComponents',
                fileName: (format) => `enterprise-components.${format}.js`,
                formats: ['es', 'umd']
            },
            rollupOptions: {
                external: ['vue', 'ant-design-vue'],
                output: {
                    globals: {
                        vue: 'Vue',
                        'ant-design-vue': 'antd'
                    }
                }
            }
        } : {
            target: 'es2015',
            outDir: 'dist',
            assetsDir: 'assets',
            rollupOptions: {
                output: {
                    manualChunks: {
                        vendor: ['vue', 'vue-router', 'pinia'],
                        antd: ['ant-design-vue', '@ant-design/icons-vue'],
                        utils: ['axios', 'dayjs', 'lodash-es']
                    }
                }
            }
        },

        server: {
            port: 3000,
            open: true,
            proxy: {
                '/api': {
                    target: env.VITE_API_BASE_URL || 'http://localhost:8080',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, '')
                }
            }
        },

        define: {
            __VUE_OPTIONS_API__: true,
            __VUE_PROD_DEVTOOLS__: false,
        }
    }
}) 